<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form ref="dialogForm" :model="form" :rules="rules" label-width="120px" label-position="left">
			<el-form-item label="排序号">
				<el-input-number v-model="form.sortNo" placeholder="=排序号"></el-input-number>
			</el-form-item>
			<el-form-item label="数据采集标题" prop="title">
				<el-input v-model="form.title" placeholder="数据采集标题" maxlength="40"></el-input>
			</el-form-item>
			<el-form-item label="源流程名" prop="flowId">
				<el-select v-model="form.flowId" clearable style="width: 100%">
					<el-option v-for="item in flowOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="目标表名" prop="targetTable">
				<el-input v-model="form.targetTable" placeholder="目标表名" maxlength="180"></el-input>
			</el-form-item>
			<el-form-item label="数据映射" prop="dataMap">
				<el-input v-model="form.dataMap" placeholder="数据映射" maxlength="380" type="textarea" :rows="5" :show-word-limit="true"
				></el-input>
				<div class="el-form-item-msg">如 d_xxx1:field1,d_xxx2:field2,格式为"流程表字段:数据表字段"</div>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" active-value="1" :nactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" maxlength="380" placeholder="备注" type="textarea" :show-word-limit="true"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
			<el-button @click="visible=false">取消</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: '新增数据采集对话框',
				edit: '编辑数据采集对话框'
			},
			flowOptions:[],
			form: {
				recordId: "",
				sortNo: 0,
				title: '',
				flowId: '',
				targetTable: '',
				dataMap: '',
				status: 1,
				remark: ''
			},
			visible: false,
			isSaveing: false,
			rules: {
				title: [
					{required: true, message: '数据采集标题不能为空', trigger: 'blur'}
				],
				targetTable: [
					{required: true, message: '目标表名不能为空', trigger: 'blur'}
				],
				flowId: [
					{required: true, message: '源流程名称不能为空', trigger: 'blur'}
				],
				dataMap: [
					{required: true, message: '数据映射不能为空', trigger: 'blur'}
				]
			},
		}
	},
	mounted() {
		this.getBpmFlowListForSelect();
	},
	methods: {
		async getBpmFlowListForSelect()
		{
			var res = await this.$API.bpm.bpmFlow.getBpmFlowListForSelect.get({model:'1'});
			if(res.code == 200){
				this.flowOptions = res.data;
			}else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.bpm.bpmDataCapture.insertBpmDataCapture.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.bpm.bpmDataCapture.updateBpmDataCapture.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.recordId = data.recordId;
			this.form.sortNo = data.sortNo;
			this.form.title = data.title;
			this.form.flowId = data.flowId;
			this.form.targetTable = data.targetTable;
			this.form.dataMap = data.dataMap;
			this.form.status = data.status;
			this.form.remark = data.remark
		}
	}
}
</script>

<style>
</style>
